<script setup>
import PageContainer from '@/components/PageContainer.vue'
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelSelect from '@/components/ChannelSelect.vue'
import { articleDeleteService, articleGetListService } from '@/api/article.js'
import { formatTime } from '../../utils/format.js'
import ArticleEdit from '@/components/ArticleEdit.vue'
import { ElMessage } from 'element-plus'

defineOptions({
  name: 'ArticleManage'
})

const articleList = ref([]) // 文章列表
const total = ref(0) // 数据总条数
const isLoading = ref(false) // loading状态

// 定义请求参数
const params = ref({
  pagenum: 1, // 当前页码数
  pagesize: 5, // 每页数据条数
  cate_id: '', // 分类id
  state: '' // 文章状态
})

// 获取文章列表
const getArticleList = async () => {
  isLoading.value = true
  const result = await articleGetListService(params.value)
  articleList.value = result.data.data
  total.value = result.data.total
  isLoading.value = false
}
getArticleList()

// 分页页码发生变化
const onCurrentChange = async (page) => {
  params.value.pagenum = page
  await getArticleList()
}

// 分页每页数据条数变化
const onSizeChange = async (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  await getArticleList()
}

// 根据文章分类以及发布状态搜索文章
const onSearch = async () => {
  params.value.pagenum = 1
  await getArticleList()
}

// 重置搜索框
const onReset = async () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  await getArticleList()
}

// 获取文章发布与编辑抽屉组件
const articleEditRef = ref()

// 添加文章
const onAddArticle = () => {
  articleEditRef.value.openDrawer({})
}

// 编辑文章
const onEditArticle = (row) => {
  articleEditRef.value.openDrawer(row)
}

// 添加和编辑成功的回调
const onSuccess = (type) => {
  if (type === 'add') {
    //  如果是添加渲染最后一页数据
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}

// 删除文章
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      // 确认删除
      await articleDeleteService(row.id)
      await getArticleList()
      ElMessage({
        type: 'success',
        message: '删除成功!'
      })
    })
    .catch(() => {
      // 取消删除
      ElMessage({
        type: 'info',
        message: '已取消删除'
      })
    })
}
</script>

<template>
  <PageContainer title="文章管理">
    <template #extra>
      <el-button @click="onAddArticle" type="primary">发布文章</el-button>
    </template>
    <template #default>
      <!-- 头部搜索表单 -->
      <el-form inline>
        <el-form-item label="文章分类：">
          <!-- modelValue 和 @update:modelValue -->
          <ChannelSelect v-model="params.cate_id"></ChannelSelect>
        </el-form-item>
        <el-form-item label="发布状态：">
          <el-select v-model="params.state" style="width: 180px">
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="草稿" value="草稿"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
          <el-button type="info" plain @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table
        v-loading="isLoading"
        :data="articleList"
        :row-style="{ height: '90px' }"
        style="width: 100%"
      >
        <el-table-column prop="title" label="文章标题">
          <template #default="scope">
            <el-link type="primary" :underline="false">{{
              scope.row.title
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="cate_name" label="分类" width="200px" />
        <el-table-column prop="pub_date" label="发布时间">
          <template #default="{ row }">
            {{ formatTime(row.pub_date) }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template #default="scope">
            <el-tag>{{ scope.row.state || '暂无状态' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100px">
          <template #default="{ row }">
            <el-button
              @click="onEditArticle(row)"
              type="primary"
              circle
              plain
              :icon="Edit"
            ></el-button>
            <el-button
              @click="onDeleteArticle(row)"
              type="danger"
              circle
              plain
              :icon="Delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 3, 5, 10]"
        :background="true"
        layout="jumper,total, sizes, prev, pager, next"
        :total="total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
        style="margin-top: 40px; justify-content: flex-end"
      />

      <!-- 发布文章抽屉  -->
      <ArticleEdit @success="onSuccess" ref="articleEditRef"></ArticleEdit>
    </template>
  </PageContainer>
</template>

<style scoped></style>
